package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.base.MVPBaseActivity;
import com.jianyun.jyzs.bean.SelectedContactInfo;
import com.jianyun.jyzs.presenter.SelectedPresenter;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.ISelectedView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import cn.rongcloud.rce.lib.utils.DefaultPortraitGenerate;
import io.rong.imkit.widget.AsyncImageView;

/**
 * Created by yangshijun on 2018/7/16.
 */

public class SelectedContactActivity extends MVPBaseActivity<ISelectedView,SelectedPresenter>
        implements ISelectedView {

    private List<SelectedContactInfo> contactModelList = new ArrayList<>();
    private SelectedContactAdapter adapter;
    private ActionBar actionBar;
    private static final int SEARCH_SELECTED_CONTACT = 1;
    private boolean isFromPin;
    private LinkedHashMap<String, SelectedContactInfo> selectedContactInfoList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rce_activity_selected_contact);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SelectedContactAdapter();
        recyclerView.setAdapter(adapter);
        selectedContactInfoList = SelectContact2Activity.selectedContactInfoList;
        Collection<SelectedContactInfo> contactModels = selectedContactInfoList.values();
        for (Iterator<SelectedContactInfo> it = contactModels.iterator(); it.hasNext(); ) {
            SelectedContactInfo contactInfo = it.next();
            if (contactInfo.isCheckable()) {
                contactModelList.add(contactInfo);
            }
        }
        actionBar.setTitle(String.format(getString(R.string.rce_selected_contacts_count), contactModelList.size()));
    }

    @NonNull
    @Override
    public SelectedPresenter createPresenter() {
        return new SelectedPresenter();
    }

    private class SelectedContactAdapter extends RecyclerView.Adapter {
        @Override
        public int getItemViewType(int position) {
            return R.layout.rce_image_list_item;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(viewType, parent, false);
            return new SelectedContactViewHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            SelectedContactViewHolder viewHolder = (SelectedContactViewHolder) holder;
            viewHolder.update(position);
        }

        @Override
        public int getItemCount() {
            return contactModelList.size();
        }
    }

    private class SelectedContactViewHolder extends RecyclerView.ViewHolder {
        private AsyncImageView portraitImageView;
        private CheckBox checkBox;
        private TextView titleTextView;
        private TextView detailTextView;
        private ImageView deleteImageView;

        SelectedContactViewHolder(View itemView) {
            super(itemView);
            portraitImageView = (AsyncImageView) itemView.findViewById(R.id.rce_portrait);
            checkBox = (CheckBox) itemView.findViewById(R.id.rce_checkbox);
            titleTextView = (TextView) itemView.findViewById(R.id.rce_title);
            detailTextView = (TextView) itemView.findViewById(R.id.rce_detail);
            deleteImageView = (ImageView) itemView.findViewById(R.id.rce_delete);
            deleteImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position >= 0) {
                        SelectedContactInfo contactInfo = contactModelList.get(position);
                        SelectContact2Activity.selectedContactInfoList.remove(contactInfo.getId());
                        contactModelList.remove(contactInfo);
                        SelectContact2Activity.selectedContactInfoList.remove(contactInfo.getId());
                        adapter.notifyItemRemoved(position);
                        actionBar.setTitle(String.format(getString(R.string.rce_selected_contacts_count), contactModelList.size()));
                    }
                }
            });
        }

        void update(int position) {
            SelectedContactInfo contactInfo = contactModelList.get(position);
            titleTextView.setVisibility(View.VISIBLE);
            checkBox.setVisibility(View.GONE);
            detailTextView.setVisibility(View.GONE);
            deleteImageView.setVisibility(View.VISIBLE);
            portraitImageView.setVisibility(View.VISIBLE);
            if (contactInfo != null) {
                String portrait = contactInfo.getPortraitUrl();
                if (TextUtils.isEmpty(portrait)) {
                    portrait = DefaultPortraitGenerate.generateDefaultAvatar(contactInfo.getName(), contactInfo.getId());
                }
                portraitImageView.setAvatar(Uri.parse(portrait));
                titleTextView.setText(contactInfo.getName());
                if (!TextUtils.isEmpty(contactInfo.getDepartName())) {
                    detailTextView.setVisibility(View.VISIBLE);
                    detailTextView.setText(contactInfo.getDepartName());
                }
            }
        }
    }

    @Override
    public void onBackClick() {
        setResult(RESULT_OK);
        super.onBackClick();
    }

    @Override
    public void onBackPressed() {
        onBackClick();
    }

    @Override
    public void onCreateActionBar(ActionBar actionBar) {
        this.actionBar = actionBar;
        actionBar.setOptionDrawable(getResources().getDrawable(R.drawable.rce_ic_nav_option_search));
        actionBar.setBackDrawable(getResources().getDrawable(R.drawable.rce_ic_nav_back));
    }

    @Override
    public void onOptionClick() {
        ArrayList<String> remainingList = new ArrayList<>();
        for (SelectedContactInfo contactInfo : contactModelList) {
            remainingList.add(contactInfo.getId());
        }
//        Intent intent = new Intent(SelectedContactActivity.this, SearchActivity.class);
//        intent.putExtra(SearchActivity.TYPE, SearchActivity.TYPE_SELECTED_CONTACT_MANAGEMENT);
//        intent.putStringArrayListExtra(SearchActivity.SELECTED_CONTACT_LIST, remainingList);
//        startActivityForResult(intent, SEARCH_SELECTED_CONTACT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SEARCH_SELECTED_CONTACT) {
                contactModelList.clear();
                Collection<SelectedContactInfo> contactModels = SelectContact2Activity.selectedContactInfoList.values();
                for (Iterator<SelectedContactInfo> it = contactModels.iterator(); it.hasNext(); ) {
                    SelectedContactInfo contactInfo = it.next();
                    if (contactInfo.isCheckable()) {
                        contactModelList.add(contactInfo);
                    }
                }
                actionBar.setTitle(String.format(getString(R.string.rce_selected_contacts_count), contactModelList.size()));
                adapter.notifyDataSetChanged();
            }
        }
    }
}
